﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace TorrentLaunch
{
    public partial class MainForm : Form
    {
        string fileName;
        Configuration config;

        public MainForm(String[] args)
        {
            InitializeComponent();
            config = new Configuration();
            config.Load();
            fileName = args[0];
            if (!fileName.Contains('\\'))
            {
                fileName = Directory.GetCurrentDirectory() + "\\" + fileName;
            }            

            torrentFile.Text = fileName.Substring(fileName.LastIndexOf('\\')+1);
        }

        private void copyToNMT_Click(object sender, EventArgs e)
        {
            string destFile = config.nmtWatchPath + fileName.Substring(fileName.LastIndexOf('\\'));
            try
            {
                File.Copy(fileName, destFile);
            }
            catch(Exception exception)
            {
                MessageBox.Show(this, "Failed to copy " + fileName + " to " + destFile + "\r\nReason: " + exception.Message, "TorrentLaunch Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            Close();
        }

        private void downloadLocal_Click(object sender, EventArgs e)
        {
            Process.Start(config.localClientExe, "\"" + fileName + "\"");
            Close();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void settingsButton_Click(object sender, EventArgs e)
        {
            ConfigForm form = new ConfigForm();
            form.Config = config;
            if (form.ShowDialog() == DialogResult.OK)
            {
                config = form.Config;
                config.Save();
            }
        }
    }
}
